package com.emm.yixun.mobile.ui.customer;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.emm.yixun.mobile.R;
import com.emm.yixun.mobile.adapter.MultiInterfaceAdapter;
import com.emm.yixun.mobile.application.EmmApplication;
import com.emm.yixun.mobile.base.BaseActivity;
import com.emm.yixun.mobile.model.GetDictionaryList.RqBusNumList;

/**
 * 
 * <p>
 * <b>Title：</b> 新增客户多选公用界面
 * </p>
 * <p>
 * <b>Description：</b> 1【生活服务】2【生活配套】3【乐于参加的活动类型】4【兴趣爱好】5【日常信息获取途径】
 * </p>
 * 
 * 
 * @author tangxin
 * @date 2017年2月17日 上午10:11:21
 */
public class MultiInterfaceActivity extends BaseActivity {

	private static final String TAG = "MultiInterfaceActivity";

	/** 侧滑关闭框架 */
	// private SwipeBackLayout mSwipeBackLayout;

	/** 标题 */
	TextView title_main;

	/** 返回 */
	ImageView back_btn;

	/** 取消 */
	Button cancel_btn;

	/** 确定 */
	Button sure_btn;

	/** 界面类型 1【生活服务】2【生活配套】3【乐于参加的活动类型】4【兴趣爱好】5【日常信息获取途径】 */
	int InterfaceType;

	/** 选择列表 MultInterfaceList */
	ListView MultInterfaceList;

	/** 选择列表适配器 */
	MultiInterfaceAdapter adapter;

	/** 生活服务 */
	ArrayList<RqBusNumList> LifeService;

	/** 生活配套 */
	ArrayList<RqBusNumList> SupportingLife;

	/** 乐于参加的活动类型 */
	ArrayList<RqBusNumList> ATypeOfActivityThatIsWillingToParticipate;

	/** 兴趣爱好 */
	ArrayList<RqBusNumList> HobbyList;

	/** 日常信息获知途径 */
	ArrayList<RqBusNumList> DailyInformationLearningApproach;

	List<String> IdList = new ArrayList<String>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.multiinterfaceactivity);

		// SetSwipeBackLayout(mSwipeBackLayout);

		BaseActivity.activity = this;

		InterfaceType = getIntent().getIntExtra("InterfaceType", 1);

		initView();

		SetRelativiLayoutPadding(R.id.main_head_top_rela);

		IdList.clear();

		LifeService = EmmApplication.LifeService;
		SupportingLife = EmmApplication.SupportingLife;
		ATypeOfActivityThatIsWillingToParticipate = EmmApplication.ATypeOfActivityThatIsWillingToParticipate;
		HobbyList = EmmApplication.HobbyList;
		DailyInformationLearningApproach = EmmApplication.DailyInformationLearningApproach;

		initBtn();

		switch (InterfaceType) {
		case 1:
			adapter = new MultiInterfaceAdapter(this, LifeService, InterfaceType);
			SetIdList(EmmApplication.LifeService);
			break;
		case 2:
			adapter = new MultiInterfaceAdapter(this, SupportingLife, InterfaceType);
			SetIdList(EmmApplication.SupportingLife);
			break;
		case 3:
			adapter = new MultiInterfaceAdapter(this, ATypeOfActivityThatIsWillingToParticipate, InterfaceType);
			SetIdList(EmmApplication.ATypeOfActivityThatIsWillingToParticipate);
			break;
		case 4:
			adapter = new MultiInterfaceAdapter(this, HobbyList, InterfaceType);
			SetIdList(EmmApplication.HobbyList);
			break;
		case 5:
			adapter = new MultiInterfaceAdapter(this, DailyInformationLearningApproach, InterfaceType);
			SetIdList(EmmApplication.DailyInformationLearningApproach);
			break;
		default:
			break;
		}

		adapter.SetIsVisible(1);

		MultInterfaceList.setAdapter(adapter);
	}

	private void initView() {
		title_main = (TextView) findViewById(R.id.title_main);
		switch (InterfaceType) {
		case 1:
			title_main.setText(getResources().getString(R.string.Life_service));
			break;
		case 2:
			title_main.setText(getResources().getString(R.string.Life_support));
			break;
		case 3:
			title_main.setText(getResources().getString(R.string.Types_of_activities_to_attend));
			break;
		case 4:
			title_main.setText(getResources().getString(R.string.Hobby));
			break;
		case 5:
			title_main.setText(getResources().getString(R.string.Daily_information_access));
			break;
		default:
			break;
		}
		back_btn = (ImageView) findViewById(R.id.back_btn);
		back_btn.setVisibility(View.GONE);
		cancel_btn = (Button) findViewById(R.id.cancel_btn);
		sure_btn = (Button) findViewById(R.id.sure_btn);
		MultInterfaceList = (ListView) findViewById(R.id.listView_s);
	}

	private void initBtn() {
		sure_btn.setVisibility(View.VISIBLE);
		sure_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// 拿到选中的项
				switch (InterfaceType) {
				case 1:
					for (int i = 0; i < EmmApplication.LifeService.size(); i++) {
						for (int j = 0; j < LifeService.size(); j++) {
							if (EmmApplication.LifeService.get(i).getDictionaryId()
									.equals(LifeService.get(j).getDictionaryId())) {
								EmmApplication.LifeService.get(i).setSelect(LifeService.get(j).isSelect());
								continue;
							}
						}
					}
					break;
				case 2:
					for (int i = 0; i < EmmApplication.SupportingLife.size(); i++) {
						for (int j = 0; j < SupportingLife.size(); j++) {
							if (EmmApplication.SupportingLife.get(i).getDictionaryId()
									.equals(SupportingLife.get(j).getDictionaryId())) {
								EmmApplication.SupportingLife.get(i).setSelect(SupportingLife.get(j).isSelect());
								continue;
							}
						}
					}
					break;
				case 3:
					for (int i = 0; i < EmmApplication.ATypeOfActivityThatIsWillingToParticipate.size(); i++) {
						for (int j = 0; j < ATypeOfActivityThatIsWillingToParticipate.size(); j++) {
							if (EmmApplication.ATypeOfActivityThatIsWillingToParticipate.get(i).getDictionaryId()
									.equals(ATypeOfActivityThatIsWillingToParticipate.get(j).getDictionaryId())) {
								EmmApplication.ATypeOfActivityThatIsWillingToParticipate.get(i).setSelect(
										ATypeOfActivityThatIsWillingToParticipate.get(j).isSelect());
								continue;
							}
						}
					}
					break;
				case 4:
					for (int i = 0; i < EmmApplication.HobbyList.size(); i++) {
						for (int j = 0; j < HobbyList.size(); j++) {
							if (EmmApplication.HobbyList.get(i).getDictionaryId()
									.equals(HobbyList.get(j).getDictionaryId())) {
								EmmApplication.HobbyList.get(i).setSelect(HobbyList.get(j).isSelect());
								continue;
							}
						}
					}
					break;
				case 5:
					for (int i = 0; i < EmmApplication.DailyInformationLearningApproach.size(); i++) {
						for (int j = 0; j < DailyInformationLearningApproach.size(); j++) {
							if (EmmApplication.DailyInformationLearningApproach.get(i).getDictionaryId()
									.equals(DailyInformationLearningApproach.get(j).getDictionaryId())) {
								EmmApplication.DailyInformationLearningApproach.get(i).setSelect(
										DailyInformationLearningApproach.get(j).isSelect());
								continue;
							}
						}
					}
					break;
				default:
					break;
				}
				finish();
			}
		});

		cancel_btn.setVisibility(View.VISIBLE);
		cancel_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Cancel();
				finish();
			}
		});

		MultInterfaceList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

				String id = adapter.GetID(InterfaceType, position);
				ArrayList<RqBusNumList> list = new ArrayList<RqBusNumList>();
				switch (InterfaceType) {
				case 1:
					for (int i = 0; i < LifeService.size(); i++) {
						if (id.equals(LifeService.get(i).getDictionaryId())) {
							LifeService.get(i).setSelect(!LifeService.get(i).isSelect());
							continue;
						}
					}
					list = LifeService;
					break;
				case 2:
					for (int i = 0; i < SupportingLife.size(); i++) {
						if (id.equals(SupportingLife.get(i).getDictionaryId())) {
							SupportingLife.get(i).setSelect(!SupportingLife.get(i).isSelect());
							continue;
						}
					}
					list = SupportingLife;
					break;
				case 3:
					for (int i = 0; i < ATypeOfActivityThatIsWillingToParticipate.size(); i++) {
						if (id.equals(ATypeOfActivityThatIsWillingToParticipate.get(i).getDictionaryId())) {
							ATypeOfActivityThatIsWillingToParticipate.get(i).setSelect(
									!ATypeOfActivityThatIsWillingToParticipate.get(i).isSelect());
							continue;
						}
					}
					list = ATypeOfActivityThatIsWillingToParticipate;
					break;
				case 4:
					for (int i = 0; i < HobbyList.size(); i++) {
						if (id.equals(HobbyList.get(i).getDictionaryId())) {
							HobbyList.get(i).setSelect(!HobbyList.get(i).isSelect());
							continue;
						}
					}
					list = HobbyList;
					break;
				case 5:
					for (int i = 0; i < DailyInformationLearningApproach.size(); i++) {
						if (id.equals(DailyInformationLearningApproach.get(i).getDictionaryId())) {
							DailyInformationLearningApproach.get(i).setSelect(
									!DailyInformationLearningApproach.get(i).isSelect());
							continue;
						}
					}
					list = DailyInformationLearningApproach;
					break;
				default:
					break;
				}
				adapter.NotifyData(list);
			}
		});

	}

	@Override
	protected void onResume() {
		super.onResume();
		BaseActivity.activity = this;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Cancel();
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}

	private void SetIdList(ArrayList<RqBusNumList> list) {
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).isSelect()) {
				IdList.add(list.get(i).getDictionaryId());
			}
		}
	}

	private ArrayList<RqBusNumList> SetApplication(ArrayList<RqBusNumList> list, List<String> Id) {
		for (int i = 0; i < list.size(); i++) {
			list.get(i).setSelect(false);
		}
		for (int i = 0; i < Id.size(); i++) {
			for (int j = 0; j < list.size(); j++) {
				if (Id.get(i).equals(list.get(j).getDictionaryId())) {
					list.get(j).setSelect(true);
				}
			}
		}
		return list;
	}

	private void Cancel() {
		switch (InterfaceType) {
		case 1:
			EmmApplication.LifeService = SetApplication(EmmApplication.LifeService, IdList);
			break;
		case 2:
			EmmApplication.SupportingLife = SetApplication(EmmApplication.SupportingLife, IdList);
			break;
		case 3:
			EmmApplication.ATypeOfActivityThatIsWillingToParticipate = SetApplication(
					EmmApplication.ATypeOfActivityThatIsWillingToParticipate, IdList);
			break;
		case 4:
			EmmApplication.HobbyList = SetApplication(EmmApplication.HobbyList, IdList);
			break;
		case 5:
			EmmApplication.DailyInformationLearningApproach = SetApplication(EmmApplication.DailyInformationLearningApproach,
					IdList);
			break;
		default:
			break;
		}
	}

}
